package com.ctgu.fastleader.controller;

import com.ctgu.fastleader.service.UserService;
import com.ctgu.fastleader.vo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    @Resource
    UserService userService;



    @RequestMapping("/register")
    public String register(){
        return "register";
    }

//    初始化
    @ResponseBody
    @RequestMapping("/allUser")
    public List<User> allUser(){
        return userService.findAllUser();
    }

//    添加用户
    @RequestMapping("/adduser")
    public String adduser(HttpServletRequest request){
        int uid = Integer.parseInt(request.getParameter("uid"));
        String upw = request.getParameter("upassword");
        String uname;
        if(request.getParameter("uname").equals("")){
            uname = "用户"+uid;
        }else {
            uname = request.getParameter("uname");
        }
        String utypename = "超级管理员";
        String uinfo = request.getParameter("uinfo");
        int utype = Integer.parseInt(request.getParameter("utype"));
        if(utype == 0){
            utypename = "超级管理员";
        }else if(utype == 1){
            utypename = "配送员";
        }
        User user = new User(uid,upw,uname,utype,utypename,uinfo);
        userService.insertSelective(user);

        return "forward:/page/toUser";
    }

//    删除用户
    @RequestMapping("/deleteUser")
    public String deleteUser(Integer uid){
        userService.deleteByPrimaryKey(uid);
        return "forward:/page/toUser";
    }

//    修改用户信息上
    @RequestMapping("/toUpdateUser")
    public String toUpdateUser(Integer uid, String uname,String uinfo, Model model){
        model.addAttribute("uid",uid);
        model.addAttribute("uname",uname);
        model.addAttribute("uinfo",uinfo);
        String upassword = userService.selectpwdbyuid(uid);
        model.addAttribute("upassword",upassword);
        return "forward:/page/toUpdateUser";
    }
//    修改用户信息下
    @RequestMapping("/updateuser")
    public String updateuser(HttpServletRequest request){
        int uid = Integer.parseInt(request.getParameter("uid"));
        String upw = request.getParameter("upassword");
        String uname;
        if(request.getParameter("uname").equals("")){
            uname = "用户"+uid;
        }else {
            uname = request.getParameter("uname");
        }
        String utypename = "超级管理员";
        String uinfo = request.getParameter("uinfo");
        int utype = Integer.parseInt(request.getParameter("utype"));
        if(utype == 0){
            utypename = "超级管理员";
        }else if(utype == 1){
            utypename = "配送员";
        }
        User user = new User(uid,upw,uname,utype,utypename,uinfo);
        userService.updateByPrimaryKeySelective(user);
        return "forward:/page/toUser";
    }

    @ResponseBody
    @RequestMapping("/allDelivery")
    public List<User> allDelivery(){
        return userService.selectUserBytype();
    }
}
